package com.fh.fruit.controller.problem;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.fruit.service.user.FruitUserManager;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;


@Controller
@RequestMapping(value="/problem")
public class ProblemController extends BaseController {

	String menuUrl = "problem/getProblemList"; //常见问题列表
	
	@Resource(name="fruitUserService")
	private FruitUserManager fruitUserService;
	
	/**列出问题列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getProblemList")
	public ModelAndView listUsers(Page page)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		page.setPd(pd);
		String id=pd.getString("id");
		List<Map<String,Object>> problemList = fruitUserService.getProblemList(page);	//列出问题列表
		if("0".equals(id)){
			pd.put("pid", 0);
		} else {
			pd.put("pid", fruitUserService.getProblemParentId(pd));
		}
		mv.setViewName("farm/problem/problem_list");
		mv.addObject("problemList", problemList);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	/**
	 * 请求编辑页面
	 * @param id
	 * @return ModelAndView
	 */
	@RequestMapping(value="/toEdit")
	public ModelAndView toEdit()throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			Map<String,Object> problem= fruitUserService.getProblem(pd);
			pd.put("id", problem.get("parent_id"));
			mv.addObject("MSG", "edit");
			mv.addObject("pd", pd);
			mv.addObject("problem", problem);
			mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
			mv.setViewName("farm/problem/problem_edit");
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 保存修改
	 * @return
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit()throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			System.out.println("problem_name=="+pd.getString("problem_name"));
			if(pd!=null){
				fruitUserService.updateEdit(pd);
			}
			mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
			mv.setViewName("redirect:/problem/getProblemList.do?id="+fruitUserService.getProblemParentId(pd)); //保存成功跳转到列表页面
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 请求新增页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toAdd")
	public ModelAndView toAdd()throws Exception{
		ModelAndView mv = this.getModelAndView();
		try{
			PageData pd = new PageData();
			pd = this.getPageData();
			String id =(String)pd.get("id");
			Map<String,Object> problem= fruitUserService.getProblemParent(pd);
			pd.put("parent_id", id);
			mv.addObject("pd", pd);	
			mv.addObject("problem", problem);	//传入菜单ID，作为子菜单的父菜单ID用
			mv.addObject("MSG", "add");							//执行状态 add 为添加
			mv.setViewName("farm/problem/problem_edit");
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 保存修改
	 * @return
	 */
	@RequestMapping(value="/add")
	public ModelAndView add()throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			if(pd!=null){
				String parent_id=pd.getString("parent_id");
				if(parent_id == null || "".equals(parent_id)){
					pd.put("parent_id", 0);
				}
				fruitUserService.saveProblem(pd);
			}
			mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
			mv.setViewName("redirect:/problem/getProblemList.do?id="+pd.get("parent_id")); //保存成功跳转到列表页面
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 删除问题
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/delProblem")
	@ResponseBody
	public String delProblem(Page page,HttpServletRequest requset) {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "del")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "删除问题");
		PageData pd = this.getPageData();
		page.setPd(pd);
		try {
			List<Map<String,Object>> problemList = fruitUserService.getProblemList(page);
			if(problemList.size()>0){
				return "NO-DEL";
			}
			fruitUserService.delProblem(pd);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "failed";
		}
	}
	
}
